﻿using M_Bom.API.Application.Command.BomCommand;
using M_Bom_Domain;
using M_Bom_Repository.Interface;
using MediatR;

namespace M_Bom.API.Application.CommandHandler.BomCommandHandler
{
    public class BomCreateCommandHandler : IRequestHandler<BomCreacteCommand, int>
    {
        protected readonly IBom bom;
        protected readonly IBomYGuan bomYGuan;

        public BomCreateCommandHandler(IBom bom)
        {
            this.bom = bom;
        }

        public BomCreateCommandHandler(IBom bom, IBomYGuan bomYGuan)
        {
            this.bom = bom;
            this.bomYGuan = bomYGuan;
        }

        public async Task<int> Handle(BomCreacteCommand request, CancellationToken cancellationToken)
        {
            await bom.AddAsync(new Bom()
            {
                Bombh = request.Bombh,
                Bombanben = request.Bombanben,
                Chenbh = request.Chenbh,
                ChenName = request.ChenName,
                Guige = request.Guige,
                Chicun = request.Chicun,
                CaozuoName = request.CaozuoName,
                ProcessRouteId = request.ProcessRouteId,
            });
          
            foreach (var item in request.IngredientId)
            {
                await bomYGuan.AddAsync(new BomYGuan()
                {
                  IngredientId=item,
                    Bombh=request.Bombh
                });
            }
            return 1;

        }
    }
}
